<?php

namespace app\admin\controller;

use think\Db;
use think\response\Json;

Class Bank extends Base
{
    public function bankedit(){
        $id = $this->request->param('id');
        $row = Db::table("tb_mt_bank")->where("id",$id)->find();
        if ($row){
            $img =get_table_field('tb_file','id',$row['img'],'save_name,file_name,type');
            $row['img_url'] = $img;
        }
        if ($this->request->isPost()) {
            $data= $this->request->post();
            $data["create_time"]=date("Y-m-d H:i:s");
            Db::table("tb_mt_bank")->where("id",$id)->update($data);
            return ['status' => "y", 'info' => '操作成功'];
        }
//        echo  json_encode($row);
        return view('', ['row' => $row]);
    }
    public function bankeadd(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data["uid"]=0;
            $data["create_time"]=date("Y-m-d H:i:s");
            $bool = Db::table("tb_mt_bank")->insert($data);
            return ['status' => "y", 'info' => '操作成功'];
        }
        return view();
    }
    public function bankrecord(){
        $bank = $this->request->get('bank');
        $where =[];
        if ($bank){
            $where['bank'] =$bank;
        }
        $list = Db::table("tb_mt_bank_money")->where($where)->paginate(10, false, ['query' => $this->request->param()]);
        $money =  Db::table("tb_mt_bank_money")->where($where)->sum("money");
        return view('', ['list' => $list,'money' => $money]);
    }

    public function banklist(){
        $type = $this->request->get('type');
        $status = $this->request->get('status');
        $payType = $this->request->get('payType');
        $mobile = $this->request->get('mobile');
        $where = array();
        //是否系统用户
        if ($type){
           if ($type==1){
               $where[] = ['uid', 'eq', 0];
           }else if ($type==2){
               $where[] = ['uid', 'neq', 0];
           }
        }
        //是否冻结
        if ($status){
            if ($status==1){
                $where[] = ['status', 'eq', 0];
            }else if ($status==2){
                $where[] = ['status', 'eq', 1];
            }
        }
        if ($payType){
            $where[] = ['type', 'eq', $payType];
        }
        if ($mobile){
            $where[] = ['mobile', 'like', '%'.$mobile.'%'];
        }
        $list = Db::table("tb_mt_bank")->where($where)->order("is_del asc")->paginate(10, false, ['query' => $this->request->param()]);
        $header =[];
        $header["t_money"]= Db::table("tb_mt_bank")->where($where)->sum("t_money");
        $header["d_money"]= Db::table("tb_mt_bank")->where($where)->sum("d_money");
        return view('', ['list' => $list,"header"=>$header]);
    }

    public function bankstatus(){
        $data = $this->request->post();
        $status = $data['status'] == 0 ? 1 : 0;
        $res = Db::table("tb_mt_bank")->where('id', $data['id'])->setField('status', $status);
        if ($res) {
            return ['status' => 'y', 'info' => '操作成功'];
        } else {
            return ['status' => 'n', 'info' => '操作失败'];
        }
    }

    public function myBanks(){
        $uid = $this->request->param('id');
        $where=[];
        $where[] = ['uid', 'eq', $uid];
        $list = Db::table("tb_mt_bank")->where($where)->order("is_del asc")->paginate(10, false, ['query' => $this->request->param()]);
        $header =[];
        $header["t_money"]= Db::table("tb_mt_bank")->where($where)->sum("t_money");
        $header["d_money"]= Db::table("tb_mt_bank")->where($where)->sum("d_money");
        return view('', ['list' => $list,"header"=>$header]);
    }

    public function bankdel(){
        $data = $this->request->post();
        $orderCount = Db::table("tb_mt_order")->where('pay_bank_id', $data['id'])->where("order_status","1,2,5")->count("id");
        if ($orderCount>0){
            return ['status' => '0', 'info' => '还用户还有订单未完成，暂无法删除'];
        }
        $otcOrderCount = Db::table("tb_mt_otc_order")->where('pay_type', $data['id'])->where("status","0,1,3")->count("id");
        if ($otcOrderCount>0){
            return ['status' => '0', 'info' => '还用户还有OTC订单未完成，暂无法删除'];
        }
        $res = Db::table("tb_mt_bank")->where('id', $data['id'])->setField('is_del', 1);
        if ($res) {
            return ['status' => '1', 'info' => '操作成功'];
        } else {
            return ['status' => '0', 'info' => '操作失败'];
        }
    }

}